﻿using System;
using System.IO;
using System.Reflection;

namespace GetPublicKeyTokenConsoleApplication {
    internal class Program {
        public static void Main(string[] args) {
            foreach (var s in args) {
                //TODO: 怎么获取拖动到 本exe 文件上的文件路径?
                Console.WriteLine(s);
            }
            
            const string topLine = "╔═════════════════════════════════════════════════════════════════════════════";
            const string leftBorder = "╠═";
            const string bottomLine = "╚═════════════════════════════════════════════════════════════════════════════";
            const string sideDivider = "═════════════════════════════════════════════════════════════════════════════";

            Console.WriteLine(topLine);
            Console.WriteLine($"{leftBorder}本程序作用:");
            Console.WriteLine($"{leftBorder} 获取.dll文件的 PublicKeyToken, VS里的 sn.exe(强名称工具) 也能获取, 但命令行新手感觉麻烦.");
            Console.Write($"{leftBorder} 请输入.dll 文件的路径, ");
            tag:
            Console.WriteLine("输入 exit 退出:");
            Console.WriteLine($@"{leftBorder} 例: E:\WorkSpace_CS\XxxSolution\packages\Xxx.1.0.0\lib\xxx\Xxx.dll");
            Console.WriteLine($"{leftBorder}     建议直接将.dll拖进来!");
            Console.WriteLine(bottomLine);
            var assemblyPath = Console.ReadLine();
            if (String.IsNullOrEmpty(assemblyPath)) {
                Console.WriteLine(topLine);
                Console.Write($"{leftBorder} 输入的路径为空, 请输入正确的.dll路径, ");
                goto tag;
            }
            if (assemblyPath == "exit") {
                return;
            }
            if (!assemblyPath.EndsWith(".dll")) {
                Console.WriteLine();
                Console.WriteLine(topLine);
                Console.Write($"{leftBorder} 输入的不是.dll文件路径, 请输入正确的.dll路径, ");
                goto tag;
            }

            // string assemblyPath = @"C:\Path\To\Sample.dll";
            Assembly assembly;
            try {
                assembly = Assembly.LoadFile(assemblyPath);
            } catch (ArgumentException e) {
                /**
                 * 例: sdfsdf.dll, 没有路径
                 */
                // Console.WriteLine(e);
                // throw;
                Console.WriteLine();
                Console.WriteLine(topLine);
                Console.Write($"{leftBorder} 加载.dll失败, 请输入正确的.dll路径, ");
                goto tag;
            } catch (FileNotFoundException e) {
                /**
                 * 例: E:\WorkSpace_CS\XxxSolution\packages\Xxx.1.0.0\lib\xxx\123.dll, 有这个路径, 但没有这个文件
                 */
                // Console.WriteLine(e);
                // throw;
                Console.WriteLine();
                Console.WriteLine(topLine);
                Console.Write($"{leftBorder} 没有在路径找到制定的.dll文件, 请检查路径是否正确, ");
                goto tag;
            }

            byte[] publicKeyToken = assembly.GetName().GetPublicKeyToken();
            //if没有签名, 为空
            string publicKeyTokenStr = BitConverter.ToString(publicKeyToken);
            string publicKeyTokenString = publicKeyTokenStr.Replace("-", "");
            if (String.IsNullOrEmpty(publicKeyTokenString)) {
                Console.WriteLine("该.dll文件还没有签名!");
            } else {
                Console.WriteLine($"PublicKeyToken={publicKeyTokenString.ToLowerInvariant()}");
            }
            Console.WriteLine();
            Console.WriteLine(topLine);
            Console.Write($"{leftBorder} 如果继续获取.dll签名, 请输入路径, ");
            goto tag;
        }
    }
}